Components.utils.import("resource://mldonkeyff/MLDonkeyDBConfig.js");

var MLDonkeyServerDataDialogController = {
	onLoad: function() {
		// Ids
		MLDonkeyServerDataDialogController.hostname = document.getElementById("hostname");
		MLDonkeyServerDataDialogController.port = document.getElementById("port");
		MLDonkeyServerDataDialogController.username = document.getElementById("username");
		MLDonkeyServerDataDialogController.password = document.getElementById("password");
		// Parameters
		MLDonkeyServerDataDialogController.mode = window.arguments[0];
		MLDonkeyServerDataDialogController.id = window.arguments[1];
		// Initializations
		if(MLDonkeyServerDataDialogController.mode == "edit") {
			var client = MLDonkeyDBConfig.getConfiguredClients()[MLDonkeyServerDataDialogController.id];
			//
			MLDonkeyServerDataDialogController.hostname.value = client.getHostname();
			MLDonkeyServerDataDialogController.port.value = client.getPort();
			MLDonkeyServerDataDialogController.username.value = client.getUsername();
			MLDonkeyServerDataDialogController.password.value = client.getPassword();
		}
	},
	onOk: function() {
		var mode = MLDonkeyServerDataDialogController.mode;
		var id = MLDonkeyServerDataDialogController.id;
		if(mode == "add") {
			MLDonkeyDBConfig.insertConfiguredClient(MLDonkeyServerDataDialogController.hostname.value,
					MLDonkeyServerDataDialogController.port.value,
					MLDonkeyServerDataDialogController.username.value,
					MLDonkeyServerDataDialogController.password.value);
		} else if(mode == "edit") {
			MLDonkeyDBConfig.updateConfiguredClient(id,
					MLDonkeyServerDataDialogController.hostname.value,
					MLDonkeyServerDataDialogController.port.value,
					MLDonkeyServerDataDialogController.username.value,
					MLDonkeyServerDataDialogController.password.value);
		} else {
			throw "Invalid argument[0] " + mode;
		}
		return true;
	},
	onCancel: function() {
		return true;
	},
}